﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;

public partial class back_AddUser : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    User user;
    Country country;
    State state;
    List<State> states = new List<State>();
    List<Country> countries = new List<Country>();
    #endregion 

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCountries();
            BindStates(1); // NOte: 1 is the Id for United States. Default loading the states of United States
            
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                fillUser(Convert.ToString(Request.QueryString["Id"]));
            }
            
        }
    }
    private void BindCountries()
    {
        country = new Country(_dataObject);
        countries = country.GetAllCountries();

        drpCountry.DataSource = countries;
        drpCountry.DataValueField = "Id";
        drpCountry.DataTextField = "Name";
        drpCountry.DataBind();
    }
    private void BindStates(int countryId)
    {
        state = new State(_dataObject);
        states = state.GetStatesByCountryId(countryId);

        drpState.DataSource = states;
        drpState.DataValueField = "Id";
        drpState.DataTextField = "Name";
        drpState.DataBind();
    }
    private void fillUser(string UserId)
    {
        user = new User(_dataObject);
        user.Id = Convert.ToInt16(UserId);
        user.GetUserById();

        if (user.Id != 0)
        {
            txtFirstName.Text = user.FirstName;
            txtMiddleName.Text = user.MiddleName;
            txtLastName.Text = user.LastName;
            txtEmailId.Text = user.Email;
            txtPhoneNo.Text = user.Phone;
            txtAddress1.Text = user.Address1;
            txtAddress2.Text = user.Address2;
            txtCity.Text = user.City;
            drpState.SelectedValue = user.State.ToString();
            drpCountry.SelectedValue = user.Country.ToString();
            txtUserName.Text = user.UserName;
            txtPassword.Attributes.Add("value", user.Password);
            txtUserName.Enabled = false;
        } 
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        user = new User(_dataObject);
        user.FirstName = txtFirstName.Text;
        user.MiddleName = txtMiddleName.Text;
        user.LastName = txtLastName.Text;
        user.Email = txtEmailId.Text;
        user.Phone = txtPhoneNo.Text;
        user.Address1 = txtAddress1.Text;
        user.Address2 = txtAddress2.Text;
        user.City = txtCity.Text;
        user.State = Convert.ToInt32(drpState.SelectedItem.Value);
        user.Country = Convert.ToInt32(drpCountry.SelectedItem.Value);
        user.UserName = txtUserName.Text;
        user.Password = txtPassword.Text;
        user.DateAdded = System.DateTime.Now;
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            user.Id = Convert.ToInt32(Request.QueryString["id"]);
            user.Update();
            clearControls();
            divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;User updated successfully!";
            divMessage.Attributes.Add("class", "alert alert-success");
        }
        else
        {
            if (checkIfUserExists(user.UserName, user.Email))
            {
                divMessage.InnerHtml = "<strong>Oh snap!</strong>&nbsp;User with same username or email address already exists!";
                divMessage.Attributes.Add("class", "alert alert-error");
            }
            else
            {
                user.Create();
                clearControls();
                divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;User inserted successfully!";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
        }
    }
    private void clearControls()
    {
        txtFirstName.Text = string.Empty;
        txtMiddleName.Text = string.Empty;
        txtLastName.Text = string.Empty;
        txtEmailId.Text = string.Empty;
        txtPhoneNo.Text = string.Empty;
        txtAddress1.Text = string.Empty;
        txtAddress2.Text = string.Empty;
        txtCity.Text = string.Empty;
        //txtState.Text = string.Empty;
        //txtCountry.Text = string.Empty;
        txtUserName.Text = string.Empty;
        txtPassword.Text = string.Empty;
    }
    private bool checkIfUserExists(string userName, string email)
    {
        User checkUser = new User(_dataObject);
        checkUser.UserName = userName;
        checkUser.Email = email;
        checkUser.GetUserByUserName();

        if (checkUser.Id != 0)
            return true;
        else
            return false;
    }
}